<?php
	/**
	 * 会员管理类
	 */
	 require_once '../lib/base.php';

	class member extends base
	{
		var $db;
		var $msg = '';

		function member()
		{
			$this->base();
			$this->db = $this->module( 'lib.db' );
		}

		function register()
		{
			$sql = "select id from meye_user where email='" . $this->post['email'] . "' ";
			$data =	 $this->db->query( $sql, 'array', true );
			if( ! empty($data['id']) )
			{
				$this->msg = '该邮箱已经注册过。';
				return false;
			}
			$ctime = time();
			$ip = ip2long($this->server['REMOTE_ADDR']);
			$lastlogin = '';
			$skin_id = 1;

			$sql = "insert into meye_user (email,password,nick,active_flag,ctime,ip,lastlogin,skin_id) values ('" . $this->post['email'] ."','" . md5($this->post['password']) . "','',1,$ctime,$ip,$ctime,1) ";
			$this->db->query($sql);

			$this->msg = '注册成功。';

			return true;
		}

		function login()
		{
			$sql = "select id,password,email from meye_user where email='" . $this->post['email'] . "' ";
			$data =	 $this->db->query( $sql, 'array', true );
			if( ! empty($data['id']) )
			{
				if( md5($this->post['password']) == $data['password'] )
				{
					$this->session['user_id'] = $data['id'];

					$lastlogin = time();
					$this->update_user_info( array('lastlogin'=>$lastlogin) );

					$this->msg = '恭喜！登录成功。';
					return true;
				}
				else
				{
					$this->msg = '用户名或密码错误。';
					return false;
				}
			}
			else
			{
				$this->msg = '用户不存在。';
				return false;
			}
		}

		function logout()
		{
			if( isset($this->session['user_id']) )
			{
				if( session_destroy() )
				{
					unset($this->session['user_id']);
					$this->msg = '退出成功';
					return true;
				}
				else
				{
					$this->msg = '退出失败';
					return false;
				}
			}
			else
			{
				$this->msg = '您没有登录。';
				return false;
			}
		}

		function update_user_info( $field=array() )
		{
			if( isset($this->post['nick']) )
			{
				$sql = "select id from meye_user where nick='" . $this->post['nick'] . "'";
				$data =	 $this->db->query( $sql, 'array', true );
				if( ! empty($data['id']) )
				{
					$this->msg = '该昵称已经在使用。';
					return false;
				}
				$nick = $this->post['nick'];
			}
			if( isset($this->post['old_password']) )
			{
				$sql = "select id from meye_user where password = '" . md5($this->post['old_password']) . "' and id='" . $this->session['user_id'] . "' ";
				$data =	 $this->db->query( $sql, 'array', true );
				if( empty($data['id']) )
				{
					$this->msg = '旧密码不正确';
					return false;
				}
			}
			if( ! empty($field) )
			{
				$field_sql = '';
				foreach($field as $key=>$val)
				{
					$field_sql .= $key . '=\'' . $val . '\',';
				}
				$field_sql = substr($field_sql, 0, -1);
			}
			$sql = "update meye_user set $field_sql where id='" . $this->session['user_id'] . "'";
			$this->db->query($sql);
			$this->msg = '修改成功。';

			return true;
		}

		function get_user_info()
		{
			$sql = "select * from meye_user where id='" . $this->session['user_id'] . "'";
			$data =	 $this->db->query( $sql, 'array', true );

			return $data;
		}
	}
?>